home *** CD-ROM | disk | FTP | other *** search
/ Champak 43 / Vol 43.iso / games / phit.swf / scripts / __Packages / CPlayingField.as < prev    next >
Encoding:
Text File  |  2007-07-13  |  13.1 KB  |  423 lines

  1. class CPlayingField extends CMovieClipFresh
  2. {
  3.    var _shuffler;
  4.    var _arrPieceSetMoves;
  5.    var _travellingPieces;
  6.    var _arrPendingPieceMoveSet;
  7.    var mc_pieceHost;
  8.    var _pieceGrid;
  9.    var mc_pieceHostHost;
  10.    var _arrGroups;
  11.    var _trayBackground;
  12.    var _frame;
  13.    var _trayExtentX = 0;
  14.    var _trayExtentY = 0;
  15.    var _tableauExtentY = 0;
  16.    var _totalExtentY = 0;
  17.    var _nPieces = 0;
  18.    var TILE_WIDTH = 50;
  19.    var _wantsBuilding = true;
  20.    var _isPlayingVictory = false;
  21.    var _nMoves = 0;
  22.    var _curPieceSetMove = -1;
  23.    var _interactive = true;
  24.    var _startTime = 0;
  25.    var _pauseStartTime = 0;
  26.    var _timeSpentNonInteractive = 0;
  27.    function CPlayingField()
  28.    {
  29.       super();
  30.       this._visible = false;
  31.       this._shuffler = new CPieceShuffler();
  32.       this._arrPieceSetMoves = new Array();
  33.       this._travellingPieces = new Array();
  34.       this._arrPendingPieceMoveSet = new Array();
  35.       this._startTime = this.GetTime();
  36.       this.SetInteractive(false);
  37.       this.stop();
  38.    }
  39.    function AddTravellingPiece(piece)
  40.    {
  41.       var _loc2_ = 0;
  42.       while(_loc2_ < this._travellingPieces.length)
  43.       {
  44.          if(piece == this._travellingPieces[_loc2_])
  45.          {
  46.             return false;
  47.          }
  48.          _loc2_ = _loc2_ + 1;
  49.       }
  50.       this._travellingPieces.push(piece);
  51.       return true;
  52.    }
  53.    function RecordTravellingPieceMovement()
  54.    {
  55.       var _loc2_ = 0;
  56.       while(_loc2_ < this._travellingPieces.length)
  57.       {
  58.          CPhitPiece(this._travellingPieces[_loc2_]).RecordLastMove();
  59.          _loc2_ = _loc2_ + 1;
  60.       }
  61.       this.FinalizePieceMoveSet();
  62.       this._travellingPieces.splice(0);
  63.       this._arrPendingPieceMoveSet = new Array();
  64.    }
  65.    function FinalizePieceMoveSet()
  66.    {
  67.       this._arrPieceSetMoves.splice(this._curPieceSetMove + 1,this._arrPieceSetMoves.length - this._curPieceSetMove - 1);
  68.       this._arrPieceSetMoves.push(this._arrPendingPieceMoveSet);
  69.       this._curPieceSetMove = this._arrPieceSetMoves.length - 1;
  70.       this._nMoves = this._nMoves + 1;
  71.    }
  72.    function get _tileWidth()
  73.    {
  74.       return this.TILE_WIDTH;
  75.    }
  76.    function get _trayWidth()
  77.    {
  78.       return this._trayExtentX;
  79.    }
  80.    function get _trayHeight()
  81.    {
  82.       return this._trayExtentY;
  83.    }
  84.    function get _minTilePos()
  85.    {
  86.       return - this._tableauExtentY;
  87.    }
  88.    function get _tableauPixelHeight()
  89.    {
  90.       return this._tableauExtentY * this._tileWidth;
  91.    }
  92.    function get _pixelWidth()
  93.    {
  94.       return this._trayExtentX * this._tileWidth;
  95.    }
  96.    function get _pixelHeight()
  97.    {
  98.       return this._totalExtentY * this._tileWidth;
  99.    }
  100.    function SetInteractive(isInteractive)
  101.    {
  102.       if(this._interactive != isInteractive)
  103.       {
  104.          this._interactive = isInteractive;
  105.          if(this._interactive)
  106.          {
  107.             if(this._pauseStartTime)
  108.             {
  109.                this._timeSpentNonInteractive += this.GetTime() - this._pauseStartTime;
  110.                this._pauseStartTime = 0;
  111.             }
  112.          }
  113.          else
  114.          {
  115.             this._pauseStartTime = this.GetTime();
  116.          }
  117.          var _loc2_ = 0;
  118.          while(_loc2_ < this._nPieces)
  119.          {
  120.             var _loc3_ = CPhitPiece(this.mc_pieceHost["piece" + _loc2_]);
  121.             _loc3_.SetInteractive(this._interactive);
  122.             _loc2_ = _loc2_ + 1;
  123.          }
  124.       }
  125.    }
  126.    function IsInBounds(tilePos)
  127.    {
  128.       return tilePos._x >= 0 && tilePos._y >= - this._tableauExtentY && tilePos._x < this._trayExtentX && tilePos._y < this._trayExtentY;
  129.    }
  130.    function IsInTray(tilePos)
  131.    {
  132.       return tilePos._x >= 0 && tilePos._y >= 0 && tilePos._x < this._trayExtentX && tilePos._y < this._trayExtentY;
  133.    }
  134.    function GetPieceAtTileLocation(tilePos)
  135.    {
  136.       return this._pieceGrid[tilePos._y * this._trayExtentX + tilePos._x];
  137.    }
  138.    function SetTileOwner(tilePos, owner)
  139.    {
  140.       FreshDebug.Assert(!!owner,"owner");
  141.       FreshDebug.Assert(!this._pieceGrid[tilePos._y * this._trayExtentX + tilePos._x],"_pieceGrid[ tilePos._y * _trayExtentX + tilePos._x ] == null OR undefined");
  142.       this._pieceGrid[tilePos._y * this._trayExtentX + tilePos._x] = owner;
  143.    }
  144.    function ClearTileOwner(tilePos)
  145.    {
  146.       this._pieceGrid[tilePos._y * this._trayExtentX + tilePos._x] = null;
  147.    }
  148.    function CleanUp()
  149.    {
  150.       this._pieceGrid = null;
  151.       if(this.mc_pieceHost)
  152.       {
  153.          this.mc_pieceHost = null;
  154.          ┬º┬ºpush(this.removeMovieClip());
  155.       }
  156.    }
  157.    function Build(trayExtentX, trayExtentY, minGroupSize, maxGroupSize)
  158.    {
  159.       FreshDebug.Trace("Build");
  160.       this.CleanUp();
  161.       this._trayExtentX = trayExtentX;
  162.       this._trayExtentY = trayExtentY;
  163.       this.mc_pieceHostHost.createEmptyMovieClip("mc_pieceHost",0);
  164.       this.mc_pieceHost = this.mc_pieceHostHost.mc_pieceHost;
  165.       FreshDebug.Assert(this.mc_pieceHost != undefined,"mc_pieceHost != undefined");
  166.       if(minGroupSize != undefined && maxGroupSize != undefined)
  167.       {
  168.          var _loc2_ = new CTrayBuilder();
  169.          this._arrGroups = _loc2_.Build(trayExtentX,trayExtentY,minGroupSize,maxGroupSize);
  170.          this.SetGroups(this._arrGroups);
  171.       }
  172.    }
  173.    function BuildPieces(arrGroups)
  174.    {
  175.       this._pieceGrid = new Array();
  176.       var _loc7_ = _root.random.GetRandom() * 360;
  177.       var _loc9_ = 360 / arrGroups.length;
  178.       var _loc3_ = 0;
  179.       while(_loc3_ < arrGroups.length)
  180.       {
  181.          var _loc5_ = this.mc_pieceHost.getNextHighestDepth();
  182.          var _loc6_ = CPhitPiece(this.mc_pieceHost.attachMovie("Piece","piece" + _loc3_,_loc5_));
  183.          var _loc4_ = MathUtil.HSVtoRGB(_loc7_,1,0.75);
  184.          _loc6_.Create(_loc4_.r,_loc4_.g,_loc4_.b,arrGroups[_loc3_]);
  185.          _loc7_ += _loc9_;
  186.          _loc3_ = _loc3_ + 1;
  187.       }
  188.       this._nPieces = arrGroups.length;
  189.    }
  190.    function CreateTableau()
  191.    {
  192.       this._tableauExtentY = Math.ceil(this._trayExtentY * 1.5);
  193.       this._totalExtentY = this._tableauExtentY + this._trayExtentY;
  194.    }
  195.    function AreAllPiecesOutsideOfTray()
  196.    {
  197.       var _loc2_ = 0;
  198.       while(_loc2_ < this._nPieces)
  199.       {
  200.          var _loc3_ = CPhitPiece(this.mc_pieceHost["piece" + _loc2_]);
  201.          if(!_loc3_.IsCompletelyOutsideOfTray())
  202.          {
  203.             return false;
  204.          }
  205.          _loc2_ = _loc2_ + 1;
  206.       }
  207.       return true;
  208.    }
  209.    function AreAllPiecesWithinTray()
  210.    {
  211.       var _loc2_ = 0;
  212.       while(_loc2_ < this._nPieces)
  213.       {
  214.          var _loc3_ = CPhitPiece(this.mc_pieceHost["piece" + _loc2_]);
  215.          if(!_loc3_.IsCompletelyInsideOfTray())
  216.          {
  217.             return false;
  218.          }
  219.          _loc2_ = _loc2_ + 1;
  220.       }
  221.       return true;
  222.    }
  223.    function Shuffle(minIterations, maxIterations, maxStep, upBias)
  224.    {
  225.       if(!this._shuffler)
  226.       {
  227.          return undefined;
  228.       }
  229.       this._shuffler.Shuffle(this._trayExtentX,this._trayExtentY,this._tableauExtentY,this.mc_pieceHost,this._nPieces,maxStep,minIterations,upBias);
  230.       var _loc2_ = minIterations;
  231.       while(!this.AreAllPiecesOutsideOfTray() && _loc2_ < maxIterations)
  232.       {
  233.          this._shuffler.Shuffle(this._trayExtentX,this._trayExtentY,this._tableauExtentY,this.mc_pieceHost,this._nPieces,maxStep,1,upBias);
  234.          _loc2_ = _loc2_ + 1;
  235.       }
  236.       return _loc2_;
  237.    }
  238.    function PlayVictory()
  239.    {
  240.       if(this._isPlayingVictory)
  241.       {
  242.          return undefined;
  243.       }
  244.       FreshDebug.Trace("PlayVictory");
  245.       this._isPlayingVictory = true;
  246.       this.SetInteractive(false);
  247.       this.gotoAndPlay("victory");
  248.    }
  249.    function OnVictoryDone()
  250.    {
  251.       FreshDebug.Trace("OnVictoryDone");
  252.       _root._game.OnVictoryDisplayFinished();
  253.       this.stop();
  254.    }
  255.    function WantsBuilding()
  256.    {
  257.       return this._wantsBuilding;
  258.    }
  259.    function IsAwake()
  260.    {
  261.       return true;
  262.    }
  263.    function SaveTrace(msg)
  264.    {
  265.       FreshDebug.Trace(msg,"field-code");
  266.    }
  267.    function Save(iLevel)
  268.    {
  269.       this.SaveTrace("\tfunction StartCreatingLevel" + iLevel + "() : CPlayingField");
  270.       this.SaveTrace("\t{");
  271.       this.SaveTrace("\t\treturn _root._game.CreatePlayingField( " + iLevel + ", " + this._trayWidth + ", " + this._trayHeight + ", FinishCreatingLevel" + iLevel + " );");
  272.       this.SaveTrace("\t}");
  273.       this.SaveTrace("");
  274.       this.SaveTrace("\tfunction FinishCreatingLevel" + iLevel + "( fieldHost : CFieldHost, width : Number, height : Number ) : CPlayingField");
  275.       this.SaveTrace("\t{");
  276.       this.SaveTrace("\t\tFreshDebug.Trace( \"FinishCreatingLevel" + iLevel + "\" );");
  277.       this.SaveTrace("");
  278.       this.SaveTrace("\t\tvar playingField : CPlayingField = fieldHost._field;");
  279.       this.SaveTrace("\t\tplayingField.Build( width, height );");
  280.       this.SaveTrace("");
  281.       this.SaveTrace("\t\tplayingField.SetGroups(");
  282.       this.SaveTrace("\t\t\t[");
  283.       var _loc6_ = 0;
  284.       while(_loc6_ < this._arrGroups.length)
  285.       {
  286.          var _loc4_ = this._arrGroups[_loc6_];
  287.          var _loc3_ = "\t\t\t\t[";
  288.          var _loc2_ = 0;
  289.          while(_loc2_ < _loc4_.length)
  290.          {
  291.             var _loc5_ = _loc4_[_loc2_];
  292.             _loc3_ += " {_x: " + _loc5_._x + ", _y: " + _loc5_._y + " }";
  293.             if(_loc2_ < _loc4_.length - 1)
  294.             {
  295.                _loc3_ += ",";
  296.             }
  297.             _loc2_ = _loc2_ + 1;
  298.          }
  299.          _loc3_ += " ]";
  300.          if(_loc6_ < this._arrGroups.length - 1)
  301.          {
  302.             _loc3_ += ",";
  303.          }
  304.          this.SaveTrace(_loc3_);
  305.          _loc6_ = _loc6_ + 1;
  306.       }
  307.       this.SaveTrace("\t\t\t]");
  308.       this.SaveTrace("\t\t);");
  309.       this.SaveTrace("");
  310.       var _loc7_ = 0;
  311.       while(_loc7_ < this._nPieces)
  312.       {
  313.          var _loc9_ = CPhitPiece(this.mc_pieceHost["piece" + _loc7_]);
  314.          var _loc8_ = _loc9_._tilePosition;
  315.          this.SaveTrace("\t\tplayingField.PositionPiece( " + _loc7_ + ", " + _loc8_._x + ", " + _loc8_._y + " );");
  316.          _loc7_ = _loc7_ + 1;
  317.       }
  318.       this.SaveTrace("");
  319.       this.SaveTrace("\t\treturn playingField;");
  320.       this.SaveTrace("\t}");
  321.    }
  322.    function SetGroups(arrGroups)
  323.    {
  324.       this._arrGroups = arrGroups;
  325.       this.BuildPieces(arrGroups);
  326.       this.CreateTableau();
  327.       this._trayBackground._x = this._x - 2;
  328.       this._trayBackground._y = this._y - 2;
  329.       this._trayBackground._width = this._tileWidth * this._trayWidth + 4;
  330.       this._trayBackground._height = this._tileWidth * this._trayHeight + 4;
  331.       this._wantsBuilding = false;
  332.       this._visible = true;
  333.    }
  334.    function PositionPiece(iPiece, x, y)
  335.    {
  336.       var _loc2_ = CPhitPiece(this.mc_pieceHost["piece" + iPiece]);
  337.       _loc2_.MoveToPosition(new Vector2D(x,y),true);
  338.    }
  339.    function RememberPieceBasePositions()
  340.    {
  341.       var _loc2_ = 0;
  342.       while(_loc2_ < this._nPieces)
  343.       {
  344.          var _loc3_ = CPhitPiece(this.mc_pieceHost["piece" + _loc2_]);
  345.          _loc3_.RecordBaseSteadyPosition();
  346.          _loc2_ = _loc2_ + 1;
  347.       }
  348.    }
  349.    function RecordPieceMove(piece, startLoc, endLoc)
  350.    {
  351.       var _loc2_ = {piece:piece,startLoc:startLoc,endLoc:endLoc};
  352.       this._arrPendingPieceMoveSet.push(_loc2_);
  353.    }
  354.    function IsUndoAvailable()
  355.    {
  356.       return this._curPieceSetMove >= 0;
  357.    }
  358.    function IsRedoAvailable()
  359.    {
  360.       return this._curPieceSetMove + 1 < this._arrPieceSetMoves.length;
  361.    }
  362.    function UndoLastPieceMove()
  363.    {
  364.       if(this._curPieceSetMove >= 0)
  365.       {
  366.          var _loc4_ = this._arrPieceSetMoves[this._curPieceSetMove--];
  367.          var _loc3_ = 0;
  368.          while(_loc3_ < _loc4_.length)
  369.          {
  370.             var _loc2_ = _loc4_[_loc3_];
  371.             FreshDebug.Assert(_loc2_.piece._tilePosition.IsEqual(_loc2_.endLoc),"move.piece._tilePosition.IsEqual( move.endLoc )");
  372.             _loc2_.piece.MoveToPosition(_loc2_.startLoc,true);
  373.             _loc3_ = _loc3_ + 1;
  374.          }
  375.       }
  376.    }
  377.    function RedoLastPieceMove()
  378.    {
  379.       if(this._curPieceSetMove + 1 < this._arrPieceSetMoves.length)
  380.       {
  381.          var _loc4_ = this._arrPieceSetMoves[++this._curPieceSetMove];
  382.          var _loc2_ = _loc4_.length - 1;
  383.          while(_loc2_ >= 0)
  384.          {
  385.             var _loc3_ = _loc4_[_loc2_];
  386.             _loc3_.piece.MoveToPosition(_loc3_.endLoc,true);
  387.             _loc2_ = _loc2_ - 1;
  388.          }
  389.       }
  390.    }
  391.    function ResetAllMoves()
  392.    {
  393.       while(this._curPieceSetMove >= 0)
  394.       {
  395.          this.UndoLastPieceMove();
  396.       }
  397.    }
  398.    function GetNumMovesMade()
  399.    {
  400.       return this._nMoves;
  401.    }
  402.    function GetTimeTaken()
  403.    {
  404.       if(this._startTime)
  405.       {
  406.          var _loc2_ = this.GetTime();
  407.          return _loc2_ - this._startTime - this._timeSpentNonInteractive - (this._pauseStartTime <= 0 ? 0 : _loc2_ - this._pauseStartTime);
  408.       }
  409.       return undefined;
  410.    }
  411.    function GetTime()
  412.    {
  413.       return new Date().getTime() / 1000;
  414.    }
  415.    function SizeFrame(scale)
  416.    {
  417.       this._frame._x = this._x;
  418.       this._frame._y = this._y - this._tableauPixelHeight - 1;
  419.       this._frame._width = this._pixelWidth + 28 / scale;
  420.       this._frame._height = this._pixelHeight + 4;
  421.    }
  422. }
  423.